﻿using Iworld.Backstage.Framework.GameService;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using System;
namespace Iworld.Backstage
{
    public partial class EditGameRoundWindow : ChildWindow
    {

        

        public EditGameRoundWindow()
        {
            InitializeComponent();
        }

        void ShowMsg(string error,Action act=null)
        {
           ErrorPrompt ep= new ErrorPrompt() { Info = error};
            if(act != null)
            {
                ep.Closed += (sender, args) => {
                    act();
                };
            } 
            ep.Show();
        }


        public GameRoundExport GameRoundInfo
        {
            get { return (GameRoundExport)GetValue(GameRoundInfoProperty); }
            set { SetValue(GameRoundInfoProperty, value); }
        }

          public static readonly DependencyProperty GameRoundInfoProperty =
            DependencyProperty.Register("GameRoundInfo", typeof(GameRoundExport), typeof(EditGameRoundWindow),
            new PropertyMetadata(null, (sender, args) =>
            {
                EditGameRoundWindow erw = sender as EditGameRoundWindow;
                erw.Color.SelectedIndex = (int)erw.GameRoundInfo.PokerColor;
                erw.Number.SelectedIndex =(int)erw.GameRoundInfo.PokerNumber -1;
            }));

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(Color.SelectedIndex < 0 || Number.SelectedIndex < 0)
            {
                this.ShowMsg("请选择完整的信息");
            }
            PokerColor pc = (PokerColor)Color.SelectedIndex;
            PokerNumber pn = (PokerNumber)(Number.SelectedIndex+1);
            GameServiceClient client = new GameServiceClient();
            client.EditGameRoundCompleted += client_EditGameRoundCompleted;
            client.EditGameRoundAsync(new EditGameRoundImport()
            {
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                ,
                PokerColor = pc,
                PokerNumber = pn,
                RoundId = GameRoundInfo.RoundId
            }, new TempInfo() { Color=pc,Number=pn});
        }

        void client_EditGameRoundCompleted(object sender, EditGameRoundCompletedEventArgs e)
        {
             if(e.Result.Success)
             {
                 TempInfo ti = e.UserState as TempInfo;
                 GameRoundInfo.PokerColor = ti.Color;
                 GameRoundInfo.PokerNumber = ti.Number;
                 ShowMsg("编辑成功!", () => { this.DialogResult = true; });
             }else
             {
                 ShowMsg(e.Result.Error);
             }
        }

        class TempInfo
        {
            public PokerColor Color{set;get;}
            public PokerNumber Number { set; get; }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


    }
}

